package com.chat.controller;

import java.io.File;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.*;
import com.chat.domain.User;
import com.chat.domain.validator.UserValidator;
import com.chat.param.JsonContent;
import com.chat.param.LoginParam;
import com.chat.param.JsonContent.Header;
import com.chat.service.UserService;
import com.chat.util.ControllerUtils;

@Controller
@RequestMapping("/login")
// /user/**
public class LoginController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(LoginController.class);

	
	@Resource
	private UserService userService;

	@RequestMapping(value = "/view")
	public String loginView(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "login";
	}

	@RequestMapping(value = "/action")
	public @ResponseBody 
	String login(HttpServletRequest request, HttpServletResponse response,User userParam) {
		String username = userParam.getUsername().toLowerCase();	
	
		userParam.setUsername(username);

		String result = "";
		log.debug("user="+userParam);
		JsonContent jsonContent = validate(userParam, new UserValidator());

		if (jsonContent.isInvalidate()) {
			result = JSON.toJSONString(jsonContent);
			return result;
		}
		jsonContent = userService.canUserLogin( userParam);
		result = JSON.toJSONString(jsonContent);
		return result;
	}

	

	

	@RequestMapping(value = "/view/index")
	public
	String Index(HttpServletRequest request, HttpServletResponse response,User userParam) {
		return "index";
	}
}